/*
 * adc.c
 *
 *  Created on: Feb 17, 2013
 *      Author: justin
 */

#include "project.h"

uint8_t readAdc(uint8_t adcPin) {

	ADMUX &= 0xF0;						//Clear the older channel that was read
	ADMUX |= adcPin;					//Defines the new ADC channel to be read
	ADCSRA |= (1 << ADSC);				//Starts a new conversion
	while (ADCSRA & (1 << ADSC)) {
	}		//wait while converter busy
	return ADCW ;

}

void initAdc(uint8_t adcPin) {

	ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));    //per datasheet, 16Mhz/128(prescale)=125Khz the ADC reads
	ADMUX |= (1 << REFS0);		//use 5vVref
	ADCSRA |= (1 << ADEN);		//enable adc free run mode
	ADCSRA |= (1 << ADSC);

}
